#include "head.h"

int initSocket(int *socket_fd)
{
    *socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    // 设置端口复用
    int reuse = 1;
    setsockopt(*socket_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));

    // 绑定ip和端口
    char ip[100] = {0};
    load_config("ip", ip);
    char port[100] = {0};
    load_config("port", port);

    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_addr.s_addr = inet_addr(ip);
    sockaddr.sin_port = htons(atoi(port));
    bind(*socket_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));

    listen(*socket_fd, 10);

    return 0;
}
